<template>
  <div class="pagination">
      <button :disabled="pageNo==1" @click="$emit('crruentpage',pageNo-1)">上一页</button>
    <button v-if="start>1" @click="$emit('crruentpage',1)">1</button>
    <button  v-if="start>2">...</button>

  <template v-for="(page,index) in end" :key="index">
    <button  v-if="page>=start" @click="$emit('crruentpage',page)" :class="{active:pageNo==page}" >{{page}}</button>
  </template>
    <button  v-if="end<pages-1">···</button>
    <button  v-if="end<pages" @click="$emit('crruentpage',pages)" :class="{active:pageNo==pages}">{{pages}}</button>
    <button :disabled="pageNo==pages" @click="$emit('crruentpage',pageNo+1)">下一页</button>
    
    <button style="margin-left: 30px">共{{total}}条</button>
  </div>
</template>

<script>
import {computed,ref} from 'vue'
  export default {
    props:['total',"pageNo",'pageSize','contiues'],
    name: "Pagination",
    setup(props) {
      //使用计算属性出展示数据需要的页数，注意！！要计算属性的值和ref一样需要.value(只读的)
      let pages=computed(()=>{
        return Math.ceil(props.total/props.pageSize)//MATH.ceil方法：向上取整
      })
      // 根据当前页计算出起始页和结束页
      let start=ref(0)//起始页码
      let end =ref(0)//结束页码
      // 异常情况，开头数据少页数不够
        if(pages.value<props.contiues){
              start=1
              end=pages.value
        }
        else{
          start=props.pageNo-parseInt(props.contiues/2)
          end =props.pageNo.value+parseInt(props.contiues/2)
          if (start<1) {// start页数小于1
            start=1
            end=props.contiues
          }
      
         if(end>pages.value){    // end页数大于总页数
            start =pages.value-props.contiues+1
            end=pages.value
          }
        }

      return{
        pages,start,end
      }
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
      text-align: center;
    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: #606266;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #409eff;
        color: #fff;
      }
    }
  }
</style>